package org.jet.emall.prod.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.log.AdminOperationLog;
import org.jet.emall.log.LogType;
import org.jet.emall.prod.dto.BrandDTO;
import org.jet.emall.prod.dto.BrandDetailsDTO;
import org.jet.emall.prod.dto.NewBrandDTO;
import org.jet.emall.prod.dto.UpdateBrandDTO;
import org.jet.emall.prod.model.NewBrandModel;
import org.jet.emall.prod.model.UpdateBrandModel;
import org.jet.emall.prod.service.CustomBrandService;
import org.jet.emall.prod.transfer.BrandVoTransfer;
import org.jet.emall.prod.vo.BrandDetailsVO;
import org.jet.emall.prod.vo.BrandVO;
import org.jet.emall.prod.wrapper.BrandIdAndCateIdWrapper;
import org.jet.emall.prod.wrapper.BrandIdAndStatusWrapper;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-16 16:56
 */
@Api(value = "品牌管理Controller",tags = "品牌操作接口")
@RestController
public class BrandController {

    @Autowired
    private CustomBrandService brandService;

    @ApiInformation(function = "分页查找品牌",menu = "品牌管理")
    @ApiOperation(value="分页查找品牌")
    @RequestMapping(value = "/brands",method = RequestMethod.GET)
    public CommonResult<CommonPage<BrandVO>> page(String query, Integer pageNum, Integer pageSize){
        CommonPage<BrandDTO> page = brandService.page(query,pageNum,pageSize);
        ((CommonPage<BrandVO>)(Object)page).setList(BrandVoTransfer.INSTANCE.toVoList(page.getList()));
        return CommonResult.success((CommonPage<BrandVO>)(Object)page,"分页查找品牌成功");
    }

    @ApiInformation(function = "搜索品牌")
    @ApiOperation(value="搜索品牌")
    @RequestMapping(value = "/brands/query",method = RequestMethod.GET)
    public CommonResult<List<BrandVO>> query(@RequestParam String query){
        List<BrandDTO> brandDTOS = brandService.query(query);
        return CommonResult.success(BrandVoTransfer.INSTANCE.toVoList(brandDTOS),"模糊查找品牌成功");
    }

    @ApiInformation(function = "获取分类的品牌")
    @ApiOperation(value="获取分类的品牌")
    @RequestMapping(value = "/brands/cate/{cate3Id}",method = RequestMethod.GET)
    public CommonResult<List<BrandVO>> getBrandsOfCate3(@PathVariable Integer cate3Id){
        List<BrandDTO> brandDTOS = brandService.list(cate3Id);
        return CommonResult.success(BrandVoTransfer.INSTANCE.toVoList(brandDTOS),"获取分类的品牌列表成功");
    }

    @ApiInformation(function = "启用/禁用品牌",menu = "品牌管理")
    @AdminOperationLog(value = "启用/禁用品牌",logType = LogType.UPDATE)
    @ApiOperation(value = "启用/禁用品牌")
    @RequestMapping(value = "/brands/status",method = RequestMethod.PATCH)
    public CommonResult<Object> setStatusOfBrand(@RequestBody @Valid BrandIdAndStatusWrapper wrapper){
        brandService.update(wrapper.getId(),wrapper.getStatus());
        return CommonResult.success(null, "修改品牌状态成功");
    }
    @ApiInformation(function = "添加品牌",menu = "品牌管理")
    @AdminOperationLog(value = "添加品牌",logType = LogType.ADD)
    @ApiOperation(value="添加品牌")
    @RequestMapping(value = "/brands",method = RequestMethod.POST)
    public CommonResult<Object> addBrand(@RequestBody NewBrandModel brandModel){
        NewBrandDTO dto = BrandVoTransfer.INSTANCE.toDto(brandModel);
        brandService.saveBrand(dto);
        return CommonResult.success(null,"添加品牌成功");
    }
    @ApiInformation(function = "删除品牌",menu = "品牌管理")
    @AdminOperationLog(value = "删除品牌",logType = LogType.DELETE)
    @ApiOperation(value="删除品牌")
    @RequestMapping(value = "/brands",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteBrands(@RequestBody List<Integer> ids){
        if(!ids.isEmpty()) {
            brandService.deleteBrands(ids);
            return CommonResult.success(null, "删除品牌成功");
        }else{
            return CommonResult.badRequest("参数错误");
        }

    }
    @ApiInformation(function = "修改品牌",menu = "品牌管理")
    @AdminOperationLog(value = "修改品牌",logType = LogType.UPDATE)
    @ApiOperation(value="修改品牌")
    @RequestMapping(value = "/brands",method = RequestMethod.PUT)
    public CommonResult<Object> updateBrand(@RequestBody @Valid UpdateBrandModel brandModel){
        UpdateBrandDTO brandDTO = BrandVoTransfer.INSTANCE.toDto(brandModel);
        brandService.updateBrand(brandDTO);
        return CommonResult.success(null,"品牌修改成功");
    }

    @ApiInformation(function = "获取品牌详情",menu = "品牌管理")
    @ApiOperation(value = "获取品牌详情")
    @RequestMapping(value = "/brands/{id}",method = RequestMethod.GET)
    public CommonResult<BrandDetailsVO> getBrandDetails(@PathVariable Integer id){
        BrandDetailsDTO brandDetailsDTO = brandService.getBrandDetails(id);
        return CommonResult.success(BrandVoTransfer.INSTANCE.toVo(brandDetailsDTO),"获取品牌详情成功");
    }

    @ApiInformation(function = "删除品牌的分类",menu = "品牌管理")
    @AdminOperationLog(value = "删除品牌的分类",logType = LogType.DELETE)
    @ApiOperation(value="删除品牌的分类")
    @RequestMapping(value="/brands/cate",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteCateOfBrand(@RequestBody @Valid BrandIdAndCateIdWrapper wrapper){
        brandService.deleteCate(wrapper.getBrandId(),wrapper.getCateId());
        return CommonResult.success(null,"删除品牌的一个分类成功");
    }

    @ApiInformation(function = "给品牌添加分类",menu = "品牌管理")
    @AdminOperationLog(value = "给品牌添加分类",logType = LogType.ADD)
    @ApiOperation(value="给品牌添加分类")
    @RequestMapping(value="/brands/cate",method = RequestMethod.POST)
    public CommonResult<Object> addCateForBrand(@RequestBody @Valid BrandIdAndCateIdWrapper wrapper){
        brandService.addCate(wrapper.getBrandId(),wrapper.getCateId());
        return CommonResult.success(null,"给品牌添加一个分类成功");
    }

}
